package test.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import test.common.DataResult;
import test.service.YqfkdjService;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 *
 * @ project name : example
 * @ Author: XuLeHuang
 * @ Date: 2022/10/1 18:32
 * @ Description:
 */
@Controller
public class NoDependencyExcelOutPutController {
    @Autowired
    private YqfkdjService yqfkdjService;
    @RequestMapping(value = "/DM", method = RequestMethod.GET)
    public void exportKhfxxx(HttpServletResponse resp) throws Exception {
        try {
            DataResult result = new DataResult();
            SimpleDateFormat df = new SimpleDateFormat("yyyyMMddhhmmssSSS");
            result = yqfkdjService.exporYqfkdj();

            //下载
            //文件来源
            String filepath = result.getMsg().replace("\"", "");
            File file = new File(filepath);
            //下载文件文件名
            String filename = "用户信息" + df.format(new Date()) + ".xlsx";

            //固定
            InputStream fis = new BufferedInputStream(new FileInputStream(file));
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            resp.reset();
            resp.setHeader("Content-Disposition",
                    "attachment;filename=" + new String(filename.replaceAll(" ", "").getBytes("gbk")));
            resp.setHeader("Content-Length", "" + file.length());
            OutputStream os = new BufferedOutputStream(resp.getOutputStream());
            resp.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            // 输出文件
            os.write(buffer);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }
}
